package net.kazed.nextaction.database.serialize;

import java.util.List;

import net.kazed.nextaction.database.GtdContext;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.database.TaskContext;

public class ContextXmlSerializer {
   
   private XmlWriter writer;

   /**
    * Constructor.
    * @param writer XML writer.
    */
   public ContextXmlSerializer(XmlWriter writer) {
      super();
      this.writer = writer;
   }

   /**
    * Serialize context.
    * @param context Serialize this context.
    */
   public void serialize(GtdContext context) {
      writer.startElement(RestoreContext.ELEMENT_NAME);
      writer.addElement(RestoreContext.ID, context.getId());
      writer.addElement(RestoreContext.PARENT_ID, context.getParentContextId());
      writer.addElement(RestoreContext.NAME, context.getName());
      writer.endElement();
   }

   /**
    * Serialize task.
    * @param task Serialize this task.
    */
   public void serialize(Task task, List<TaskContext> taskContexts) {
      writer.startElement(RestoreTask.ELEMENT_NAME);
      writer.addElement(RestoreTask.ID, task.getId());
      writer.addElement(RestoreTask.DESCRIPTION, task.getDescription());
      writer.addElement(RestoreTask.DETAILS, task.getDetails());
      writer.addElement(RestoreTask.COMPLETE, task.getComplete());
      for (TaskContext taskContext : taskContexts) {
         writer.addElement(RestoreTask.CONTEXT_ID, taskContext.getContextId());
      }
      writer.addElement(RestoreTask.CREATED, task.getCreated());
      writer.addElement(RestoreTask.DUE_DATE, task.getDueDate());
      writer.addElement(RestoreTask.MODIFY_DATE, task.getModified());
      writer.addElement(RestoreTask.ORDER, task.getOrder());
      writer.addElement(RestoreTask.PARENT_TASK_ID, task.getParentTaskId());
      writer.addElement(RestoreTask.START_DATE, task.getStartDate());
      if (task.getStatus() != null) {
          writer.addElement(RestoreTask.TYPE, task.getStatus().getType());
      }
      writer.endElement();
   }

}
